﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Logging;
using ScrumTable.Common.Security;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.Schema.ScrumTableV1;
using ScrumTable.DL.Data.TFS.Bulk.Properties;
using ScrumTable.DL.Data.TFS.Bulk.Services.Utils;
using ScrumTable.DL.Data.TFS.Services.TeamFoundation;

using IterationBucket = ScrumTable.DL.Data.Generic.Cache.CacheElementListEntryBucket<ScrumTable.DL.Data.Generic.Cache.CacheIteration>;
using IterationSavePair = ScrumTable.DL.Data.Generic.Cache.CacheSavePair<ScrumTable.DL.Data.Generic.Cache.CacheElementListEntryBucket<ScrumTable.DL.Data.Generic.Cache.CacheIteration>>;

#endregion

namespace ScrumTable.DL.Data.TFS.Bulk.Services.IterationHandling
{
    /// <summary>
    /// This class is used to save all data below a given iteration.
    /// </summary>
    internal class Tfs11IterationSaver : IterationSaverBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new TfsIterationSaver with the associated schema config.
        /// </summary>
        /// <param name="schema">Specifies the schema configuration data.</param>
        internal Tfs11IterationSaver(IterationSchema schema)
            : base(schema)
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Stores the iterations to the back-end system.
        /// </summary>
        /// <param name="tfsContext">Specifies the tfs connection information.</param>
        /// <param name="changes">Specifies an instance with the data changes from the data layer.</param>
        internal override void SaveIterationData(TfsServerWrapper tfsContext, CacheDataChanges changes)
        {
            PreCondition.AssertNotNull(tfsContext, "tfsContext");
            PreCondition.AssertNotNull(changes, "changes");

            foreach (IterationBucket iterBucket in changes.ChangedIterations.Values)
            {
                if (TryStoreIteration(tfsContext, iterBucket))
                {
                    changes.IterationsToSync[iterBucket.Element.UniqueId].State = CacheSavePairState.Saved;
                }
                else
                {
                    changes.IterationsToSync[iterBucket.Element.UniqueId].State = CacheSavePairState.Error;
                }
            }
        }

        private bool TryStoreIteration(TfsServerWrapper tfsContext, CacheElementListEntryBucket<CacheIteration> iterBucket)
        {
            try
            {
                tfsContext.StructureService.SetIterationDates(
                    iterBucket.Element.BackEndId,
                    iterBucket.Element.StartDate,
                    iterBucket.Element.EndDate);

                return true;
            }
            catch (Exception e)
            {
                Logger<DataSaveProcess>.Error(
                    string.Format(
                        Resources.ExcDataSaveProcIterSaveFailed,
                        (iterBucket.Element != null) ? iterBucket.Element.Name : string.Empty,
                        (iterBucket.Element != null) ? iterBucket.Element.Id : CacheIdentity.DefaultId),
                    e);
                return false;
            }
        }

        #endregion
    }
}